using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Microsoft.Xna.Framework.Graphics;
using Core.Graphics;
using SerializationUtilities;

namespace WorldEditor
{
    public partial class ImportTileSet : Form
    {
        GraphicsDevice _Device;
        TileSetTypes _TileSetType;

        public ImportTileSet(GraphicsDevice device, TileSetTypes TileSettype)
        {
            InitializeComponent();

            _Device = device;
            _TileSetType = TileSettype;
        }

        private void btnPickFile_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                txtFilePath.Text = openFileDialog1.FileName;
                //Change accept button
                this.AcceptButton = btnImport;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            SerializationUtilities.Utilities<TileSet> Utilities = new SerializationUtilities.Utilities<TileSet>();
            //Deserialize file to object
            TileSet ts = Utilities.ReadObjectFromFile(openFileDialog1.OpenFile(), SerializationType.XML, CompressionType.SevenZip);
            //Load the texture given the graphics device, and set the ID, and load the tiles
            ts.AfterLoadFromFileProcedure(_Device);
            //Since this is an import give this TileSet a new ID, and then updat NextID;
            ts.ID = TileSetCollection.NextID;
            TileSetCollection.NextID = ts.ID;
            //add our imported tile set
            TileSetCollection tsc = TileSetLoaderHelper.GetCollectionForType(_TileSetType);
            tsc.Add(ts.ID, ts);
            //Close the import window
            this.Close();
        }
    }
}